package 动态规划专项训练.动态规划训练2;

/**剑指OfferII089.房屋偷盗
 * @author zx
 * @create 2022-03-31 20:42
 */
public class Offer089 {
    public int rob(int[] nums) {
        int n = nums.length;
        if(n == 0){
            return 0;
        }
        int[] f = new int[n + 1];
        f[0] = 0;
        f[1] = nums[0];
        for(int i = 2;i <= n;i++){
            f[i] = Math.max(f[i - 1],f[i - 2] + nums[i - 1]);
        }
        return f[n];
    }
    public int dynamic(int[] nums) {
        int n = nums.length;
        int[] f = new int[n + 1];
        f[0] = 0;
        int l = 0;
        f[l + 1] = nums[l];
        for (int i = l + 2; i <= n; i++) {
            f[i] = Math.max(f[i - 1], f[i - 2] + nums[i - 1]);
        }
        return f[n];
    }
}
